define(['router','api','user','util'],function(Router,api,user,util){
	var location=window.location,
	    hash=location.hash,
	    router,
	    currentController = null,
	    routes = {	    	
		    '/': 'pages/index/index',// 首页（产品列表）
		    '/index': 'pages/index/index',// 首页（产品列表）
		    '/index/subscribe':'pages/index/subscribe',//关注公众号
		    '/index/productshow/:code':'pages/index/productshow',//商品展示期
		    '/index/productdetail/:code':'pages/index/productdetail',//认购
			'/index/producttrade/:code':'pages/index/producttrade', //交易期期商品
			'/order/transactionrecord':'pages/order/transactionrecord', // 成交记录
			'/order/subscriprecord':'pages/order/subscriprecord', // 认购记录
			'/order/ltrecord':'pages/order/ltrecord', // 挂单记录
			'/order':'pages/order/index', // 订单
			'/order/pickup/:code':'pages/order/pickup', // 提货
			'/order/transfer/:code':'pages/order/transfer', // 定向交易
			'/notice':'pages/notice/index', //公告列表
			'/notice/detail/:id':'pages/notice/detail', //公告详情
			'/login':'pages/user/login', // 登录
			'/agent/:uid/:pid':'pages/user/agent',// 记录扫码推荐人和机构
			'/logout':function(){ //退出
			   api.loginOut({
			      success:function(){
			         user.loginOut();
			      },
			      error:function(){
			      }
			   })
			},
			'/user':'pages/user/index',//个人中心首页
			'/user/detail':'pages/user/detail', //修改密码
			'/user/card':'pages/user/card', //个人信息
			'/user/customer':'pages/user/customer', //客户信息
			'/user/commission':'pages/user/commission', //佣金分成
			'/user/register/:fromto':'pages/user/register', // 注册
			'/user/register':'pages/user/register',
			'/user/search':'pages/user/search', //信息查询（用户功能列表）
			'/user/newaddress':'pages/user/newaddress', //新增收获和地址
			'/pass/getlogin':'pages/pass/getlogin', //忘记登录密码
			'/pass/getfund':'pages/pass/getfund', //忘记支付密码
			'/user/risk':'pages/user/risk', //风险揭示书
			'/fund/flow':'pages/fund/flow', //资金流水
			'/fund/bind/:id/:backurl':'pages/fund/bind', // 绑银行卡
			'/fund/charge/:backurl':'pages/fund/charge',// 充值
			'/fund/cash/:backurl':'pages/fund/cash', //提现
			'/bank/list':'pages/bank/list', // 银行信息
			'/bank/channel':'pages/bank/channel', // 银行通道选项
			'/article/rule':'pages/article/rule', // 规则
			'/user/share': 'pages/user/share',//分享
			'/test':function(){
				//测试本地存储兼容性
//				localStorage.setItem('t1', 'localStorage');
//				$.cookie('t2','cookie');
//				
//				if(window.sessionStorage){
//					alert(sessionStorage.getItem('t1'));
//					alert($.cookie('t2'));
//				}else{
//					alert($.cookie('t2'));
//				}
				
				// 检测是苹果还是安卓
				var ua = navigator.userAgent.toLowerCase();	
				if (/iphone|ipad|ipod/.test(ua)) {
					    alert("iphone");		
				} else if (/android/.test(ua)) {
					    alert("android");	
				}
			},
			'/*':'pages/index/error',
			'/*/*':'pages/index/error'
	    },
	    //用于把字符串转化为一个函数，而这个也是路由的处理核心
	    routeHandler = function (config) {
	    	
	    	if(typeof(config)!=='string')return config;
	        return function () {
	            var url = config,
	                params = arguments;
	            require([url], function (controller) {
	                if(currentController && currentController !== controller){
	                    currentController.onRouteChange && currentController.onRouteChange();
	                }
	                currentController = controller;
	                controller.init.apply(controller, params);
	            });
	        }
	    },
	    boot=function(){
	        for (var key in routes) {
			    routes[key] = routeHandler(routes[key]);
			}
			router = Router(routes);
			router.init();
			api.router=router;
			if(''===hash)location.hash="#/";
	    };
	    
	api.init(boot);
})